from TikTokApi import TikTokApi
import json
from datetime import datetime
import random
import string
proxies = {
  "http": "http://scraperapi:7383d26700b0269bfe37eaadc1eab541@proxy-server.scraperapi.com:8001",
  "https": "http://scraperapi:7383d26700b0269bfe37eaadc1eab541@proxy-server.scraperapi.com:8001"
}
verifyfp='verify_ko0fd9ox_7zB6ZPGL_TBiM_4pN5_ABqn_VrcivzeHnVpk'
# api = TikTokApi.get_instance(custom_verifyFp=verifyfp)

proxy="http://208.80.28.208:8080"
#proxy="http://209.126.4.134:3128"
#proxy="http://100.24.216.83:80"
#proxy="http://52.35.63.184:20052"
#proxy="http://3.114.241.246:8080"
#proxy="http://104.248.123.76:18080"
#proxy="http://104.248.194.15:8080"
#proxy="http://159.65.125.104:3128"
#proxy="http://72.76.238.130:3128"
#proxy="http://64.71.159.172:3128"
#proxy="http://104.156.251.217:8080"
#proxy="http://134.209.218.104:8080"
#proxy="http://157.245.64.99:8080"
#proxy="http://157.230.6.23:8080"
#proxy="http://134.209.119.195:8080"
#proxy="http://157.245.252.104:8080"
#proxy="http://134.122.20.165:8080"
#proxy="http://157.230.85.89:8080"

#proxy="http://157.230.208.88:8080"
#proxy="http://157.230.227.106:8080"
#proxy="http://148.72.152.156:3128"
#proxy="http://143.198.222.22:8080"
#proxy="http://104.236.81.226:8080"

#proxy="http://165.225.50.142:10801"
#proxy="http://208.110.64.202:3128"
#proxy="http://198.24.170.194:32108"
#proxy="http://165.225.50.142:10801"
#proxy="http://104.236.123.137:8080"
#proxy="http://104.236.205.169:8080"
#proxy="http://104.236.195.241:8080"
#proxy="http://104.236.81.226:8080"
#proxy="http://157.90.140.250:8080"
#proxy="http://140.238.228.223:3128"
#proxy="http://207.244.241.139:3128"
#proxy="http://3.139.235.232:3128"
#proxy="http://140.238.228.223:3128"



did = str(random.randint(10000, 999999999))
api = TikTokApi.get_instance(use_test_endpoints=True,  proxy=proxy, custom_did=did)
import os
# i=0
# userlist=api.bySound('6820654145293273090 ',count=5000)
# with open('sound6820654145293273090_5000_new.json', 'w', encoding='utf-8',newline='\n') as filename_output:
#     for tt in userlist:
#         i+=1
#         print(i)
#         filename_output.write(json.dumps(tt)+'\n')

# tiktokData = api.get_Video_By_Url(
# #     "https://v16-web-newkey.tiktokcdn.com/bc1d826cb318146aeef927858a602e1b/5f8a5898/video/tos/useast2a/tos-useast2a-ve-0068c001/84577df0c6cd402b87cb3865a3ea85af/?a=1988&br=2916&bt=1458&cr=0&cs=0&cv=1&dr=0&ds=3&er=&l=2020101620355701019017601435096290&lr=tiktok_m&mime_type=video_mp4&qs=0&rc=M2V1NDt5bHBpdjMzNjczM0ApZjVpM2hoNDs0N2U5ZjdoOGdxYWQta2ZtbjFfLS0wMTZzc2EwLmEtY2A0MF8uNjJgLzY6Yw%3D%3D&vl=&vr="
# # )1ById('6848002044318584069')['itemInfo']['itemStruct']["video"]["downloadAddr"])
# with open("sample1.mp4", "wb") as out:
#     out.write(tiktokData)
#htlist=api.discoverHashtags(did=did,proxy=proxy,custom_verifyFp=verifyfp)
# ht=['#ComingOfAge', '#falldiy', '#worldseries', '#gaminglife', '#meleaving', '#yellow']
# ht=['#festivefashion', '#selfimprovement','#happyhanukkah','#planttiktok','#productivity','#ImAGhost','#catchphrases','#ootd','#comfortfood','#holidaycrafts','#watchmegrow','#holidaydecor','#raisedby','#makeitvogue','#goodmorning','#gamenight','#HorrorToK', '#yougotthis','#mycostume','#nbaisback','#perfectgifts','#artmas','#hellowinter','#Wishlist','#haventseen','#RoomTour','#bakingszn','#catchphrases','#rnbvibes',
#   '#christmas2020','#feliznavidad','#timewarpjump','#holidaytreats','#nonuancenovember','#gamingsetup','#carsoftiktok','#needtoknow','#whenwewereyounger','#halloweenishere','#ourtype','#workingathome','#onlinedating','#familyrecipe','#givingthanks','#diceroll','#holidayvibes','#holidaytiktok','#recordsday','#inkdrawing','#happyholidays','#homecooked','#neonshadow',  '#youwantmore' ,'#familyimpression','#givingszn','#whereilive','foodtiktok', '#personalfinance', '#tiktokfood',
#   '#thinkingabout','#healthyheart','#imbusyrightnow','#womeninsports','#fetapasta','#albumcover','#stepbystep','#makeblackhistory','#relationshipstorytime','#emophase','#wintersports','#RoyalRumble','#feelinggood','#xgamesmode','#roundofapplause','#typing','#tiktokdiy','#winterbeautytips','#clingypet','#meditation101','#couplethings','#OlympicsCountdown','#cocinando','#tiktoktutorial','#notaperfectperson','#WinterMagic','#plantparent','#timewarpwaterfall','#foodie','#joblife','#mlkday','#2021affirmations','#cleantok','#visionboard','#seashanty','#homeimprovement','#zodiacsign','#skincare101','#groupchat','#winterfashion','#NHLFaceOff','#nbadraft','#lovestory','fallfashion','#planttiktok','#OhNo','#animation','#tabletop','#homeoffice','#myrecommendation',
#     '#healthycooking','#moneytok','#weirdpets','#wee','#fitnessroutine','#withouttellingme','#myhaul','#Bye2020','#welcome2021','#nflplayoffs','#GreenScreenScan','#tortillatrend','#gamergoals','#rareaesthetic', '#hyperfixated','#whatilearned','#dailyvlog','#easydiy','graphicdesign','#mystyle','#bekind','smallbusiness','#growupwithme','#selfimprovement','#fallguysmoments',
#     '#RatatouilleMusical','#coldweather','#inlove','#housetour','#homemade','#fanedit','#stemlife','#winterfit', '#interiordesign','#holidaymusic','#howbizarre','#festivefashion','#motivationmonday','#isthisavailable','#wip','#myroutine','#veteransday', '#productivity','#sfxmakeup', '#ComingOfAge', '#falldiy','#gaminglife','#readysetshop', '#meleaving','#nativefamily','#YearOnTikTok','#holidaycountdown','#WordsOfWisdom','#happyhalloween','#onhold','#halloweenlook','#easyrecipe','#cozyathome',
#     '#worldseries','#holidaysourway','#theatrekids','#wrappinggifts','#welldone','#WeekendVibes','#mypfp', '#yellow','#projectcar','#wildanimals','#myhobby']
#ht=['graphicdesign','smallbusiness','#homeoffice','#myrecommendation','#myhobby','#fanedit','#stemlife','#motivationmonday','#wip','#veteransday','#holidaycountdown','#WeekendVibes','#holidaysourway','#RoomTour','#bakingszn','#nbadraft']w
# ht=[ '#YearOnTikTok','#hellowinter', '#lovestory', '#artmas','#festivefashion','#happyhanukkah','#planttiktok','#productivity','#ImAGhost','#catchphrases','#ootd','#comfortfood','#holidaycrafts','#watchmegrow','#holidaydecor','#raisedby','#makeitvogue','#goodmorning','#catchphrases','#rnbvibes','#gamenight','#HorrorToK', '#ProblemSolved', '#tiktokfood', '#interiordesign', '#sfxmakeup', '#ComingOfAge', '#falldiy', '#worldseries', '#gaminglife', '#meleaving', '#yellow', '#yougotthis','#mycostume','#mypfp','#welldone','#nativefamily','#happyhalloween','#homeoffice','#myrecommendation','#myhobby','#fanedit','#stemlife','#motivationmonday','#wip','#veteransday','#holidaycountdown','#WeekendVibes','#holidaysourway','#RoomTour','#bakingszn','#nbadraft',
#      '#nonuancenovember','#gamingsetup','#carsoftiktok','#needtoknow','#whenwewereyounger','#halloweenishere','#ourtype','#fallguysmoments','#workingathome','#onlinedating','#familyrecipe','#givingthanks','#diceroll','#holidayvibes','#holidaytiktok','#recordsday','#inkdrawing','#happyholidays','#homecooked','#neonshadow',  '#youwantmore' ,'#familyimpression','#givingszn','#whereilive','foodtiktok','#holidaymusic','#howbizarre', '#personalfinance'
#      ,'#thinkingabout', '#housetour','#readysetshop','#easydiy','#coldweather','#wildanimals','#festivefashion','graphicdesign','smallbusiness','#halloweenlook','fallfashion','#artmas','#haventseen','#planttiktok','#productivity','#bekind','#onhold','#growupwithme','#theatrekids','#wishlist','#haventseen','#selfimprovement', '#OhNo','#animation','#tabletop']
# ht=['#UnwrapTheDeals','#ShowUpShowOff','#MicellarRewind','#HeinzHalloween']'#rnbvibes', '#halloweenishere' '#ourtype' '57419989', ,'#halloweenlook','#youwantmore','#animation', '#youwantmore','#diwali', '#smallbusiness'  '#recordsday', '#rnbvibes', ,'#diwali'  ,'whereilive','graphicdesign', '#youwantmore'  '#familyimpression','#youwantmore',,'#foodtiktok' '#watchmegrow'  '#oneyourthere', '#RatatouilleMusical'
# ht=['#bakingszn','#halloweenishere','#RoomTour']'#nbaisback','#perfectgifts','#thinkingabout', '#housetour','#readysetshop','#easydiy','#coldweather','#wildanimals','#festivefashion'

ht=[
    #"#gamingsetup", '#blackandproud','#laughingduet','#yoga101',
    #'#fantheory','#tiktokfitness',
    #'#nbaallstar','#beautyhack','#homediy','#homecook','#Lifestyle','#ontherunway','#dayandnight','#tiktokwildlifeday','#thriftflip','#science101','#seitan','#gamingtiktok', '#whenwomenwin','#glasspainting', '#foodontiktok',
    #'#upcycling','#somethingyoulearned','#thankstoher',"#naturescereal",'#SabiasQue','#90saesthetic','#nowlookatthis','#defrosting','#cartiktok','#crowdcheers','#homecafe',
    #'#athletesoftiktok','#herstory','#competitivegaming','#pleasedontgo','#fitnesslife','#marchmadness',
    #'#sheearnedit','#dinnerparty','#colorblast','#takeout','#thrifting','#vinylclub','#lookbook','#cleaningtiktok',
    #'#stpatricksday','#hellospring','#nowruz','#inspirationalquotes','#puppiesonly','#sidetable','#homebusiness','#stylehacks','#ecohacks','#mobilegame','#tipsandtricks','#tiktokcars','#earthday','#Inverted',
    #'#passover','#passingthephone','#happyholi','#gettheretogether','#learnontiktok','#doctorsoftiktok','#sheesh','#healthyliving','#springvibes','#wrestlemania','#2018vs2021','#diyprojects','#graphicdesign','#itwasntaphase',
    #'#siblinggoals','#guitartok','#learnwithme','#happyeaster','#arabtiktok','#aprilfools','#baseballisback','#transvisibilityday','#amongus','#givethanksnotpranks','#autismawareness',"#baseballtiktok",'#sehablaespañol',
    '#vaccinatedfor','#songfacts','#downsyndromeday','#oscars','#promszn','#springoutfit','#neontwin']

ht=['#2018vs2021', '#amongus', '#aprilfools', '#arabtiktok', '#autismawareness', '#baseballisback', '#baseballtiktok', '#cleaningtiktok', '#diyprojects', '#earthday', '#ecohacks',
    '#givethanksnotpranks', '#guitartok', '#happyeaster', '#healthyliving', '#Inverted', '#itwasntaphase', '#learnwithme', '#lookbook', '#mobilegame', '#sehablaespañol', '#sheesh',
    '#siblinggoals', '#songfacts', '#springvibes', '#takeabite', '#thrifting', '#tiktokcars', '#tipsandtricks', '#transvisibilityday', '#vaccinatedfor', '#vinylclub', '#wrestlemania']

#ht=['#2018vs2021', '#baseballtiktok', '#cleaningtiktok', '#diyprojects', '#earthday', '#ecohacks', '#Inverted', '#itwasntaphase', '#lookbook', '#mobilegame', '#sehablaespañol', '#songfacts', '#thrifting', '#tiktokcars', '#tipsandtricks', '#vaccinatedfor', '#vinylclub']
t='2021-04-29'
numht={}
n=1

for item in htlist:
    key=item['cardItem']['title'].replace('#','')
    numht.update({key:n})
    n+=1
    if item['cardItem']['title'] not in ht:
        ht.append(item['cardItem']['title'])
        print('Add:',item['cardItem']['title'])
n=1

print(len(htlist))
print(ht)
for item in htlist:
    print(item['cardItem']['title'].replace('#',''))
for hashtag in ht:
    h=hashtag.replace('#','')
    k=0
    if h in numht.keys():
        k=numht[h]
    if 'Hashtag_' + h + '_' + str(t) +'_'+str(k)+ '.json' not in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        hashtags=api.byHashtag(h,did=did,custom_verifyFp=verifyfp,count=2000,proxy=proxy)

        with open('D:\\Work\\Tool\\tiktok\\TikToks\\Hashtag_'+h+'_'+str(t)+'_'+str(k)+'.json', 'w', encoding='utf-8', newline='\n') as filename_output:
            i=0
            for tt in hashtags:
                i+=1
                filename_output.write(json.dumps(tt)+'\n')
            print(h,i,k)

